<?php
/* [DoYouHaoBaby!] (C)Dianniu From 2010.
   this($)*/

!defined('DYHB_PATH') && exit;

class TemplateGtkGladeWidgetCompiler_GtkNotebook extends TemplateGtkGladeWidgetCompilerBase{

	private $_oPageContentWidget=null;

	public function compile(TemplateObj $oObject){
		parent::compile($oObject);
		$sCompiled=$oObject->getCompiled();
		$sVariableName='$'.TemplateGtkGlade::getWidgetVariableName($oObject);
		$sCompiled.="\t\tif({$sVariableName}->get_n_pages())";// 设置第一页为当前页
		$sCompiled.="\t\t\t{$sVariableName}->set_current_page(0);";
		$oObject->SetCompiled($sCompiled);
	}

	protected function compileCreateWidget_(TemplateGtkGladeWidget $oObject){
		$sWidgetClass=$oObject->getWidgetClass();
		$sVariableName='$'.TemplateGtkGlade::getWidgetVariableName($oObject);
		$sCompiled="\t\t{$sVariableName}=new {$sWidgetClass}();";
		if($sValue=TemplateGtkGladeObj::nodeTextToValue($oObject->getProperty('tab_pos'))){// 标签位置
			$sValue=str_replace('GTK_','Gtk::',$sValue);
			$sCompiled.="\t\t{$sVariableName}->set_tab_pos($sValue);";
		}
		if(($sValue=($oObject->getProperty('show_tabs')))!==null){// 显示标签
			$sCompiled.="\t\t{$sVariableName}->set_show_tabs($sValue);";
		}
		if(($sValue=($oObject->getProperty('show_border')))!==null){// 显示边框
			$sCompiled.="\t\t{$sVariableName}->set_show_border($sValue);";
		}
		if(($sValue=($oObject->getProperty('scrollable')))!==null){// 滚动
			$sCompiled.="\t\t{$sVariableName}->set_scrollable($sValue);";
		}
		if(TemplateGtkGladeObj::nodeTextToValue($oObject->getProperty('enable_popup'))){// 弹出菜单
			$sCompiled.="\t\t{$sVariableName}->popup_enable();";
		}
		return $sCompiled;
	}

	protected function compileChildWidgetPacking_(TemplateGtkGladeWidget $oParentWidget,TemplateGtkGladeWidget $oChildWidget,TemplateGtkGladeWidgetPacking $oPacking){
		$sParentVariableName='$'.TemplateGtkGlade::getWidgetVariableName($oParentWidget);// 父/子 窗体变量名
		$sChildWidgetVariableName='$'.TemplateGtkGlade::getWidgetVariableName($oChildWidget);
		$sChildWidgetName=$oChildWidget->getWidgetId();// 子窗体名
		$sCompiled="\t\t{$sChildWidgetVariableName}=self::GetWidget('{$sChildWidgetName}',\$arrObjectForSignal);";// 创建 子窗体（不管它是什么，先创建）
		if($oPacking->getProperty('type')===null){// 页内容窗体(page)和 页标签窗体(tab) 按顺序出现&页内容窗体(page)
			$this->_oPageContentWidget=$oChildWidget;
			$sCompiled.="\t\t{$sParentVariableName}->append_page({$sChildWidgetVariableName});";
			if(($sValue=($oPacking->getProperty('menu_label')))!==null){// 菜单标签
				$sValue=str_replace('"','\\"',$sValue);
				$sCompiled.="\t\t{$sParentVariableName}->set_menu_label({$sChildWidgetVariableName},new GtkLabel(\"{$sValue}\"));";
			}
		}
		else if($oPacking->getProperty('type')=='tab'){// 页标签窗体(tab)
			if(!$this->_oPageContentWidget){// 空的内容窗体，创建一个空的  GktLabel，并加入到 GtkNotebook
				$sPageContentWidgetVarName='$wndEmptyLabel';
				$sCompiled.="\t\t// 创建一个空的  GktLabel，作为 页内容 窗体 加入到 GtkNotebook";
				$sCompiled.="\t\t{$sPageContentWidgetVarName}=new GtkLabel('');";
				$sCompiled.="\t\t{$sParentVariableName}->append_page({$sPageContentWidgetVarName});";
			}
			else{
				$sPageContentWidgetVarName='$'.TemplateGtkGlade::getWidgetVariableName($this->_oPageContentWidget);
			}
			$sCompiled.="\t\t{$sParentVariableName}->set_tab_label({$sPageContentWidgetVarName},{$sChildWidgetVariableName});";// 将 标签窗体 加入到  GtkNotebook
			$sExpand=$oPacking->getProperty('tab_expand');// 标签窗体的  Parking 方式
			if($sExpand===null){
				$sExpand='false';
			}
			$sFill=$oPacking->getProperty('tab_fill');
			if($sFill===null){
				$sFill='false';
			}
			$sPack=TemplateGtkGladeObj::nodeTextToValue($oPacking->getProperty('tab_pack'));
			if($sPack===null){
				$sPack='Gtk::PACK_START';
			}
			else{
				$sPack=str_replace('GTK_','Gtk::',$sPack);
			}
			$sCompiled.="\t\t{$sParentVariableName}->set_tab_label_packing({$sPageContentWidgetVarName},{$sExpand},{$sFill},{$sPack});";
			if(($sValue=($oPacking->getProperty('menu_label')))!==null){// 菜单标签
				$sValue=str_replace('"','\\"',$sValue);
				$sCompiled.="\t\t{$sParentVariableName}->set_menu_label({$sPageContentWidgetVarName},new GtkLabel(\"{$sValue}\"));";
			}
			$sCompiled.= "";
			$this->_oPageContentWidget=null;// 清除
		}
		else{// more
			A::ASSERT_(0);
		}
		return $sCompiled;
	}

	static public function regToProcessorManager(ITemplateObjProcessorManager $oManager){
		$oManager->regProcessor(str_replace('TemplateGtkGladeWidgetCompiler_','',__CLASS__),__CLASS__);
	}

}
